package com.example.base.app;

import com.alibaba.fastjson.JSONObject;
import com.example.base.utils.RequestUtils;
import com.example.base.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * app 应用拦截器
 */
@Slf4j
@Component
public class AppInterceptor {

    public Boolean valitedApp(HttpServletRequest request, HttpServletResponse response,HandlerMethod handlerMethod){
        String clsName=handlerMethod.getBeanType().getName();
        String appName=AppUtils.getAppName(clsName);
        if(appName==null){
            return false;
        }
        if(appName.equals(AppUtils.SYSTEM_MAIN_APP)){
            return true;
        }
        App app=AppUtils.getApp(appName);
        if(app==null){
            return false;
        }
        AppSecretInfo appSecretInfo=app.getAppSecretInfo();
        Long time=System.currentTimeMillis();
        if(time > appSecretInfo.getExpireSecond()){
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("msg","系统授权已到期, 已暂停访问");
            ResponseUtils.responseJson(response,jsonObject);
            return false;
        }
        return true;
    }
}
